package ac.svn.accesscontrol.ui;

import java.awt.Color;

import javax.swing.Icon;
import javax.swing.JTextPane;
import javax.swing.text.BadLocationException;
import javax.swing.text.Style;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyleContext;
import javax.swing.text.StyledDocument;

public class XTextPane extends JTextPane {

	public interface IStyles
	{
		public static final String STYLE_DEF 		= "STYLE_DEF";
		public static final String STYLE_LOC 		= "STYLE_LOC";
		public static final String STYLE_ACCESS 	= "STYLE_ACCESS";
		public static final String STYLE_USER 		= "STYLE_USER";
		public static final String STYLE_GROUP 		= "STYLE_GROUP";
		public static final String STYLE_HEADING 	= "STYLE_HEADING";
	}	



	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private StyledDocument document;
	private XHighlighter highlighter;
	public XTextPane()
	{
		super();
		highlighter = new XHighlighter(this);
		setEditable(false);
		installStyles();
	}

	private void installStyles() 
	{
		document = getStyledDocument();

		Style defaultStyle = document.getStyle(StyleContext.DEFAULT_STYLE);

		Style style = document.addStyle(IStyles.STYLE_DEF, defaultStyle);

		style = document.addStyle(IStyles.STYLE_LOC, defaultStyle);
		StyleConstants.setFontSize(style, 12);
		StyleConstants.setUnderline(style, true);
		StyleConstants.setForeground(style, Color.BLUE);

		style = document.addStyle(IStyles.STYLE_ACCESS, defaultStyle);
		StyleConstants.setBold(style, true);

		style = document.addStyle(IStyles.STYLE_USER, defaultStyle);
		StyleConstants.setForeground(style, Color.DARK_GRAY);
		StyleConstants.setFontSize(style, 13);
		//StyleConstants.setIcon(style, (new ImageIcon(Utility.getImage("images/user.png"))));

		style = document.addStyle(IStyles.STYLE_GROUP, defaultStyle);
		StyleConstants.setForeground(style, Color.DARK_GRAY);
		StyleConstants.setFontSize(style, 13);
		//StyleConstants.setIcon(style, (new ImageIcon(Utility.getImage("images/group.png"))));


		style = document.addStyle(IStyles.STYLE_HEADING, defaultStyle);
		StyleConstants.setFontSize(style, 14);
		StyleConstants.setBold(style, true);	
	}   

	public void addText(String text,String styleId)
	{
		insertText(text, document.getStyle(styleId));
	}

	public void addTextWithLine(String text,String styleId)
	{
		insertText(text + "\n", document.getStyle(styleId));
	}

	private void insertText(String text,Style style)
	{
		try 
		{
			document.insertString(document.getLength(), text, style);
			setCaretPosition(document.getLength());
		} 
		catch (BadLocationException e) 
		{
			e.printStackTrace();
		}
	}

	public void insertIcon(Icon icon)
	{
		if(null == icon)
		{
			return;	    
		}

		Style defaultStyle = document.getStyle(StyleContext.DEFAULT_STYLE);
		Style style  = document.addStyle(IStyles.STYLE_ACCESS, defaultStyle);
		StyleConstants.setIcon(style, icon);
		try 
		{
			document.insertString(document.getLength(), "\t", style);
			setCaretPosition(document.getLength());
		} 
		catch (BadLocationException e) 
		{
			e.printStackTrace();
		}


	}


	public void findText(String str)
	{
		if(str.isEmpty())
		{
			highlighter.removeHighlights();
		}
		else
		{
			highlighter.highlight(str);
		}

	}

}
